package jasonllinux.com.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Activity01 extends Activity {
    /** Called when the activity is first created. */
	
	private Button startButton;
	private Button endButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = (Button) findViewById(R.id.startButton);
        endButton = (Button) findViewById(R.id.endButton);
        startButton.setOnClickListener(new Button1ClickListener());
        endButton.setOnClickListener(new Button2ClickListener());
    }
    
    class Button1ClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			handler.post(updateHandler);
		}
    	
    }
    
    class Button2ClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			handler.removeCallbacks(updateHandler);
			
		}
    	
    }
    
    Handler handler = new Handler();
    Runnable updateHandler  = new Runnable() {
		
		@Override
		public void run() {
			System.out.println("Update Handler");
			handler.postDelayed(updateHandler, 3000);
			
		}
	};
}